//
//  NavBottomView.m
//  KEREN
//
//  Created by MW on 2023/3/20.
//

#import "NavBottomView.h"
@interface NavBottomView()
@property (nonatomic, strong) NSMutableArray *btnArrM;
@end
@implementation NavBottomView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = Whire_Color;
        self.layer.mask = [CAShapeLayer clipsToBounds:self.bounds Corners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(40, 40)];
    }
    return self;
}

-(void)setCurrentIndex:(NSInteger)currentIndex{
    _currentIndex = currentIndex;
    [self setViewLayout];
}

-(void)navBtnClick:(UIButton *)sender{
    if(sender.tag == 2){
        UserInfo *user = USERINFO;
        if(user.token.length < 1){
            [[UserInfoManager sharedInstance] loginOut];
            return;
        }
    }
    self.tabController.selectedIndex = sender.tag;
}

-(void)setViewLayout{
    DDLogInfo(@"self.tabController.selectedIndex = %ld",self.tabController.selectedIndex);
    NSArray *iconArr = @[@"nav_device",@"nav_find",@"nav_me"];
    NSArray *selecticonArr = @[@"nav_device_sel",@"nav_find_sel",@"nav_me_sel"];
    [self.btnArrM  removeAllObjects];
    for (int i = 0; i < 3; i++) {
        UIButton *navBtn = [[UIButton alloc] initWithFrame:CGRectMake(DEVICE_WIDTH / 3 * i, 0, DEVICE_WIDTH / 3, 70)];
        [navBtn setImage:[UIImage imageNamed:iconArr[i]] forState:UIControlStateNormal];
        [navBtn setImage:[UIImage imageNamed:selecticonArr[i]] forState:UIControlStateSelected];

        [self addSubview:navBtn];
        if(i == _currentIndex){
            navBtn.selected = YES;
        }else{
            navBtn.selected = NO;
        }
        navBtn.tag = i;
        [navBtn addTarget:self action:@selector(navBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.btnArrM addObject:navBtn];
    }
}
- (NSMutableArray *)btnArrM{
    if(!_btnArrM){
        _btnArrM = [NSMutableArray array];
    }
    return _btnArrM;
}

@end
